//
//  SYMainTabbar.swift
//  SingWeibo
//
//  Created by shiying on 15/12/15.
//  Copyright © 2015年 shiying. All rights reserved.
//

import UIKit

class SYMainTabbar: UITabBar {
    
    private let Count = 5
    var gotoComposerViewController = {}
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
      
        //获得button的宽
        let width = self.bounds.width / CGFloat(Count)
        
        //设置button最左边的frame
        let frame = CGRect(x: 0, y: 0, width: width, height: self.bounds.height)
        
        //定义一个索引
        var index = 0
        
        //遍历
        for view in self.subviews {
 
            if view is UIControl && !(view is UIButton) {
                //设置按钮的偏移量
                view.frame = CGRectOffset(frame, CGFloat(index) * width, 0)
                index += index == 1 ? 2 : 1
                
            }
        }
        composeButton.frame = CGRectOffset(frame, 2.0 * width, 0)
    }
    
    lazy var composeButton: UIButton = {
        let button = UIButton()
        
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //设置图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.addTarget(self, action: "jumpComposeController", forControlEvents: UIControlEvents.TouchUpInside)
        //添加按钮
        self.addSubview(button)
        
        //返回button
        return button
    }()
    
    func jumpComposeController() {
    
        self.gotoComposerViewController()
    }
    

}
